local PartnerQualityPanel = app.mvc.createPanelClass(...)
local PartnerUseExpPanel = import(".PartnerUseExpPanel")
--data =
function PartnerQualityPanel.createWithData(data, node)
    assert(node ~= nil, "PartnerLevelPanel node is nil")
    local sprite = PartnerQualityPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)

    return sprite
end


function PartnerQualityPanel.create(node)
    return PartnerQualityPanel.createWithData(nil, node)
end


function PartnerQualityPanel:init(data)
   
    self.super.init(self, data)

    local sp = cc.Sprite:createWithSpriteFrameName(Res.PartnerLevelProgressPath)

    self._progressTime = cc.ProgressTimer:create( sp )
    self._progressTime:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
    self._progressTime:setMidpoint(display.ANCHOR_POINTS[1])
    self._ui["exp"]:addChild(self._progressTime)

    self._ui["btn_up"]:addClickEventListener(function()
        if not self._data  then
            return
        end
        if self._data:isSpReward() then
            display.pushToast(L("partnerCantUseExpItem"))
            return
        end
        if not self._data:isHave() then
            display.pushToast(L("PartnerNotHave"))
            return
        end
        EM:notify(ViewEvent.Partner_QualityLvUp, self)
    end)


    self._ui["name_tips"]:setString(L("partnerString1"))
    self._ui["data_3"]:setString(L("partnerString2"))

    --两个按钮 btn_1 的名字改为合成和去获得 和 btn_2 升级
    --在没有获得当前随从的时候显示btn_1 根据当前的灵魂石数量决定按钮功能和文字
    --已经获得当前随从的时候显示btn_2

    self._ui["btn_1"]:setVisible(false)
    self._ui["btn_up"]:setVisible(false)

    self._ui["btn_1"]:addClickEventListener(function()
        if not self._data  then
            return
        end

        if not self._data:isHave() then
            local haveSoulNums = UD:getPartnerSoulCountById(self._data:getSoulStoneId())
            local needNums = self._data:getComposeNeedNum()
            if haveSoulNums >= needNums then
                --self._ui["btn_1/name"]:setString(L("激活"))
                EM:notify(ViewEvent.Partner_ComposeBySoul, self._data)
            else
                --self._ui["btn_1/name"]:setString(L("去获得"))
                EM:notify(ViewEvent.Partner_OpenGetPop, self._data)
            end
        end
    end)
end

function PartnerQualityPanel:refresh()
    if not self._data then
        return
    end
    local data = self._data
    local haveNum, lvNeedSoulStone = data:getQualityCostNum()

    if lvNeedSoulStone == 0 then
        self._ui["exp_num"]:setString("max")
        self:setProgress(100)
    else
        self._ui["exp_num"]:setString(string.format("%d/%d",haveNum,lvNeedSoulStone))
        self:setProgress(haveNum/lvNeedSoulStone*100)
    end

    --品质名称

    self._ui.img:loadTexture(data:getQualityImgPath())

    --local colorGreen = cc.c3b(0, 220, 220)
    local addProps = data:getPartnerPropShow()[data:getQuality()]
    for k, v in ipairs(addProps) do
        local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
        if k == 1 then
            self._ui.data_1:setString(string.format(L("%s%s"), name or "", value or ""))
        else
            self._ui.data_2:setString(string.format(L("%s%s"), name or "", value or ""))
        end
    end

    if data:isHave() then
        --已拥有
        self._ui["btn_up"]:setVisible(true)
        self._ui["btn_1"]:setVisible(false)
        self._ui["name"]:setVisible(true)
        self._ui["name_tips"]:setVisible(false)
        self._ui["data_3"]:setVisible(false)
        self._ui.data_1:setVisible(true)
        self._ui.data_2:setVisible(true)

        self._ui.name:setString(data:getQualityText())
        self._ui.name:setTextColor(data:getQualityColor())
        --判断当前是否达到最高品质
        if data:isMaxQuality() then
            self._ui["btn_up"]:setVisible(false)
            self._ui["tips"]:setString(L("ui_csd_590"))
            self._ui["tips"]:setVisible(true)
        else
            self._ui["btn_up"]:setVisible(true)
            self._ui["tips"]:setVisible(false)
        end
        self:setRedPoint(self._ui["btn_up"], data:canLvQuality())
    else
        --未拥有
        self._ui["btn_up"]:setVisible(false)
        self._ui["btn_1"]:setVisible(true)
        --判断当前灵魂石数量
        self._ui["name"]:setVisible(false)
        self._ui["name_tips"]:setVisible(true)
        self._ui["data_3"]:setVisible(true)
        self._ui.data_1:setVisible(false)
        self._ui.data_2:setVisible(false)
        self._ui["tips"]:setVisible(false)
    
        local haveSoulNums = UD:getPartnerSoulCountById(data:getSoulStoneId())
        local needNums = data:getComposeNeedNum()
        if haveSoulNums >= needNums then
            self._ui["btn_1/name"]:setString(L("ui_csd_286")) --激活
            self:setRedPoint(self._ui["btn_1"], true)
        else
            self._ui["btn_1/name"]:setString(L("ui_csd_560")) --去获得
            self:setRedPoint(self._ui["btn_1"], false)
        end

        if haveSoulNums >= needNums then
            self:setProgress(100)
        else
            self:setProgress(haveSoulNums/needNums*100)
        end

        self._ui["exp_num"]:setString(string.format("%s/%s", haveSoulNums, needNums))

    end
end

function PartnerQualityPanel:setRedPoint(panel, isShow)
    local posX, posY = self._ui["btn_1/dot"]:getPosition()

    local flagNode = panel:getChildByName("_new_flag_node")

    if not isShow then
        if flagNode then
            flagNode:removeFromParent()
        end
    else
        if not flagNode then
            flagNode = cc.CSLoader:createNode(Res.NewFunctionFlagNode)
            flagNode:setName("_new_flag_node")
            panel:addChild(flagNode)
            flagNode:setPosition(cc.p(posX, posY))
        end
        flagNode:getTimeline():gotoFrameAndPlay( 0, flagNode:getTimeline():getDuration(), true )
    end
end

function PartnerQualityPanel:setProgress(v)
    self._progressTime:setPercentage(v)
end

return PartnerQualityPanel